package io.shallwe._admin.line;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.jfinal.aop.Inject;
import com.jfinal.kit.Kv;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

import io.shallwe.common.model.TurnoverVehicle;
import io.shallwe.history.ParameterCurveService;
import org.apache.commons.lang3.StringUtils;

/**
   * 周转车服务
 * @author 王小伟
 * @date   2019-06-10 21:17:33 
 */
public class TurnoverVehicleService {

	private TurnoverVehicle dao=new TurnoverVehicle().dao();

	@Inject
	ParameterCurveService parameterCurveService;
	
	/**
	 * 根据id查询周转车相关信息
	 * @param id
	 * @return
	 */
	public TurnoverVehicle findById(String id) {
		return dao.findById(id);
	}
	
	/**
	 * 
	 * 根据周转车编号查询周转车相关信息
	 * @param vehicleNo
	 * @return
	 */
	public TurnoverVehicle findByVehicleNo(String vehicleNo) {
		String sql="select top 1 * from t_turnover_vehicle where vehicleNo =? order by updateTime desc";
		return dao.findFirst(sql,vehicleNo);
	}
	
	public Ret paginate(Integer offset,Integer pageSize,
						String traceCode, String rackNo, String vehicleNo) {
		Integer pageNumber=new Integer((offset.intValue()/pageSize.intValue())+1);
		Kv condition =Kv.by("traceCode", traceCode)
						.set("hangerNo",rackNo)
						.set("vehicleNo",vehicleNo);
		Page<Record> paginate = Db.template("turnoverVehicle.paginate", condition).paginate(pageNumber, pageSize);
		List<Record> newList = setProductStatus(paginate.getList());
		return Ret.ok("total", paginate.getTotalRow()).set("rows", newList);
	}

	/**
	 * @Description:   周转车历史信息
	 * @param:  [offset, pageSize, traceCode, rackNo, vehicleNo]
	 * @return: com.jfinal.kit.Ret
	 * @author : gkw
	 */
	public Ret historyPaginate(Integer offset,Integer pageSize,
						String traceCode, String rackNo, String vehicleNo) {
		Integer pageNumber=offset/pageSize+1;
		Kv condition =Kv.by("traceCode", traceCode)
				.set("hangerNo",rackNo)
				.set("vehicleNo",vehicleNo);
		Page<Record> paginate = Db.template("turnoverVehicle.historyPaginate", condition).paginate(pageNumber, pageSize);
		List<Record> newList = setProductStatus(paginate.getList());
		return Ret.ok("total", paginate.getTotalRow()).set("rows", newList);
	}
	
	public Ret save(TurnoverVehicle vehicle) {
		vehicle.setUpdateTime(new Date());
		vehicle.save();
		return Ret.ok("msg","操作成功");
	}
	
	public Ret update(TurnoverVehicle vehicle) {
		vehicle.setUpdateTime(new Date());
		vehicle.update();
		return Ret.ok("msg","操作成功");
	}
	
	public Ret delete(String id) {
		dao.deleteById(id);
		return Ret.ok("msg","操作成功");
	}

	/**
	 * @Description:  根据ID获取打印页面内容
	 * @param:  [id]
	 * @return: com.jfinal.plugin.activerecord.Record
	 * @author : gkw
	 */
	public Record findPrintPageInfo(String id){
		String materialName="";
		String turnorverVehicleSql="SELECT * FROM t_turnover_vehicle WHERE id= ?";
		Record vehicleInfo=Db.findFirst(turnorverVehicleSql,id);

		if(vehicleInfo!=null){
			String materialCode=vehicleInfo.getStr("materialCode");

			if(StringUtils.isNotBlank(materialCode)){
				String materialSql="SELECT remark FROM t_material_code_name WHERE materialCode= ?";
				Record materialNameR=Db.findFirst(materialSql,materialCode);
				if(materialNameR!=null){
					String tempName=materialNameR.getStr("remark");
					if(StringUtils.isNotBlank(tempName)){
						materialName=tempName;
					}
				}
			}
			if(parameterCurveService.checkProduct(vehicleInfo.get("barcode"))){
				vehicleInfo.set("status","");
			}else{
				vehicleInfo.set("status","不合格");
			}


		}

		vehicleInfo.set("materialName",materialName);
		return vehicleInfo;
	}

	/**
	 * @Description:   打印后将状态改变，不可重复打印
	 * @param:  [id]
	 * @return: com.jfinal.kit.Ret
	 * @author : gkw
	 */
	public Ret doPrint(String id) {
		TurnoverVehicle vehicle = findById(id);
		if(null!=vehicle) {
			vehicle.setPrintFlag(1).update();
		}
		return Ret.ok("msg","操作成功");
	}

	/**
	 * @Description: 判定与组装状态方法，显示器件是否在工艺流中异常
	 * @param:  [list]
	 * @return: java.util.List<com.jfinal.plugin.activerecord.Record>
	 * @author : gkw
	 */
	private List<Record> setProductStatus(List<Record> list){
		List<Record> newList = new ArrayList<>();
		for(int i=0;i<list.size();i++){
			Record record = list.get(i);
			if(parameterCurveService.checkProduct(record.get("barcode"))){
				record.set("status","合格");
			}else{
				record.set("status","不合格");
			}
			newList.add(record);
		}
		return newList;
	}
}
